package com.bobo.controller.back.topic;

import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dto.Page;
import com.bobo.dto.VCTopic;
import com.bobo.model.Tag;
import com.bobo.model.Topic;
import com.bobo.service.topic.ITopicService;
import com.bobo.service.topic.TopicServiceImpl;
import com.bobo.service.topicTag.ITopicTagService;
import com.bobo.service.topicTag.TopicTagServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by bobo on 16-10-30.
 */
@WebServlet(name = "Topic",urlPatterns = {"/admin/topic/*"})
public class TopicBack extends BaseServlet {

    ITopicService topicService = new TopicServiceImpl();
    @RequestMapping("/getTopics")
    public String getTopics(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;

        if(request.getParameter("current") != null) {
            current = Integer.parseInt(request.getParameter("current"));
        }

        if(request.getParameter("pageSize") != null) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }

        String sortName = request.getParameter("sortName");

        String sort = request.getParameter("sort");

        String like = request.getParameter("like");

        Page<VCTopic> page = topicService.getVCTopics(current, pageSize, sortName, sort , like);
        request.setAttribute("page",page);
        request.setAttribute("sort", sort == null ? "ASC": sort);
        request.setAttribute("sortName", sortName == null ? "id": sortName);
        request.setAttribute("like", like);

        return "jsp/back/topic/getTopics";

    }

    @RequestMapping("/get")
    public String getTopic(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        ITopicTagService topicTagService = new TopicTagServiceImpl();
        List<Tag> tags = topicTagService.getTags(id);
        Topic topic = topicService.getById(id);
        topic.setTags(tags);
        request.setAttribute("topic", topic);
        return "jsp/back/topic/get";

    }

    private void sds(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        int status = Integer.parseInt(request.getParameter("status"));
        String review = request.getParameter("review");

        Topic topic = topicService.getById(id);
        topic.setStatus(status);
        topic.setReview(review);
        topicService.update(topic);
    }

    @RequestMapping(value = "/ope", method = RequestMethod.POST)
    public String ope(HttpServletRequest request) {
        sds(request);
        return "redirect:/admin/topic/getTopics";

    }

    @RequestBody
    @RequestMapping("/opee")
    public String opee(HttpServletRequest request) {
        sds(request);
        return "true";

    }

    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Topic topic = topicService.getById(id);
        int status = Integer.parseInt(request.getParameter("status"));
        topic.setStatus(status);
        topicService.update(topic);
        return "true";
    }

    /**
     * 置顶
     * @param request
     * @return
     */
    @RequestBody
    @RequestMapping("/top")
    public String top(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Topic topic = topicService.getById(id);
        topic.setTop(2);
        boolean b = topicService.update(topic);
        if(b){
            return "true";
        }else {
            return "false";
        }

    }

    /**
     * 推荐
     * @param request top:1  推荐 2: 置顶
     * @return
     */
    @RequestBody
    @RequestMapping("/recommend")
    public String Recommend(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Topic topic = topicService.getById(id);
        topic.setTop(1);
        boolean b = topicService.update(topic);
        if(b){
            return "true";
        }else {
            return "false";
        }
    }

    @RequestBody
    @RequestMapping("/review")
    public String review(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Topic topic = topicService.getById(id);
        String review =request.getParameter("review");
        topic.setReview(review);
        topicService.update(topic);
        return "";
    }

}
